package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.JsonReader;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;


public class Login extends AppCompatActivity {
    EditText mEtUserName,mEtPassWord;
    Button mEtRePassWord;
    private static final String TAG = "Login";
    String name,password1;
    int status;
    private Button mBtnRegister;
    private String nameStr;
    private String passwordStr;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_layout);

        initView();
        initData();
        initEvent();
    }


    private void initEvent() {
        mEtRePassWord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                nameStr = mEtUserName.getText().toString().trim();
                passwordStr = mEtPassWord.getText().toString().trim();

                if (TextUtils.isEmpty(nameStr)){
            Toast.makeText(Login.this,"不能为空",Toast.LENGTH_SHORT).show();
            }
            if (TextUtils.isEmpty(passwordStr)){
            Toast.makeText(Login.this,"不能为空",Toast.LENGTH_SHORT).show();
            
            }else {
                Toast.makeText(Login.this,"登录成功",Toast.LENGTH_SHORT).show();
            }
            }
        });

        mBtnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(Login.this,"进入注册页面",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(Login.this,Register.class);
                startActivity(intent);
            }
        });

    }

    private void initData(){
        String url = "http://anli-wechat.boruiman.com/freshmenapp/user/login";
        nameStr = mEtUserName.getText().toString().trim();
        passwordStr = mEtPassWord.getText().toString().trim();

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("name","nameStr");
            jsonObject.put("passwoard","passwordStr");
        }catch (JSONException e){
            e.printStackTrace();
        }
        RequestQueue requestQueue = Volley.newRequestQueue(Login.this);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.e(TAG, "onResponse: "+jsonObject.optString("msg") );

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(Login.this,"网络错误",Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);
    }

    //    绑定控件
    private void initView() {
        mEtUserName = findViewById(R.id.et_name);
        mEtPassWord = findViewById(R.id.et_password);
        mEtRePassWord = findViewById(R.id.btn_denglu);
        mBtnRegister = findViewById(R.id.btn_zhuce);
    }
}
